/**
 * Created by thinkpad on 2017-05-30.
 */
XBox.define("form.complex", {
    title:"复合表单",
    templates: {
        main: ["<div>" +
        "            <div class='tabstrip'>" +
        "            <ul>" +
        "            <li>基本信息</li>" +
        "            <li>联系方式</li>" +
        "            <li>注册证件</li>" +
        "            </ul>" +
        "            <div class='basic-info'></div>" +
        "            <div class='connect-info'></div>" +
        "            <div class='cert-info'></div>" +
        "            </div>" +
        "            <div class='buttons' style='clear: both;'></div>" +
        "</div>"],
    },
    elements: {
        form: "div.tabstrip",
        basic: "form@div.basic-info",
        connect: "form@div.connect-info",
        cert: "form@div.cert-info",
        buttons: "div.buttons",
        notify: "span.notification",
    },
    render: function () {

        this.controls.form.kendoTabStrip().data("kendoTabStrip").select(0);
        //表单渲染
        this.formBasic = new XBox.form({
            container: this.controls.basic,
            itemClass: "form-item",
            items: {
                Code: {text: "代码", type: "text", validation: {required: true, regex: /[a-z]{6,10}/i}},
                Name: {text: "名称", type: "text", validation: {required: true}},
                RegisterDate: {text: "登记日期", type: "kendo-date", options: {format: "yyyy-MM-dd"}},
                RegisterAddress: {text: "登记地址", type: "text"},
                AddressDetail: {text: "详细地址", type: "text"},
                ProductRange: {text: "经营范围", type: "text"},
                Type: {
                    text: "企业类型", type: "select", validation: {required: true}, showDefault: true,
                    options: [{text: "生产厂家", value: 1}, {text: "国外厂家", value: 2}, {
                        text: "代理商",
                        value: 3
                    }, {text: "港澳台代理商", value: 4}]
                }
            }
        });
        this.formConnect = new XBox.form({
            container: this.controls.connect,
            itemClass: "form-item",
            items: {
                ConnectMan: {text: "法人", type: "text", validation: {required: true}},
                ConnectTel: {text: "法人电话", type: "text", validation: {required: true, formatType: "telephone"}},
                ConnectEmail: {text: "法人邮箱", type: "text", validation: {required: true, formatType: "email"}},
                ConnectMan2: {text: "联系人", type: "text"},
                ConnectTel2: {text: "联系电话", type: "text", validation: {formatType: "telephone"}},
                ConnectEmail2: {text: "电子邮箱", type: "text", validation: {formatType: "email"}},
            }
        });

        this.formCert = new XBox.form({
            container: this.controls.cert,
            itemClass: "form-item",
            items: {
                CertTypes: {
                    text: "证件类型", type: "checkboxList",
                    options: [{text: "三证合一", value: 1}, {text: "经营许可证", value: 2}, {text: "特殊药品经营证", value: 3},
                        {text: "冷藏许可证", value: 3}, {text: "还有什么证", value: 4}]
                },
            }
        }, function () {
            var that = this;
            that.certs = {};
            var certListForm = $("<div>");
            this.container.append(certListForm);
            this.items["CertTypes"].element.find("input:checkbox").change(function () {
                var val = $(this).val();
                var chked = $(this).prop("checked");
                if (chked) {
                    var ff = new XBox.form({
                        container: certListForm,
                        action: "append",
                        itemClass: "form-item",
                        items: {
                            CertNo: {text: "编号", type: "text",},
                            Date:{text:"授权日期",type:"kendo-datepair",options: {format: "yyyy-MM-dd"}},
                            Remark: {text: "备注", type: "text"},
                            Upload: {text: "", type: "kendo-upload", options: {showFileList: false}}
                        }
                    })
                    that.certs[val] = ff;
                } else {
                    var f = that.certs[val];
                    if (f) {
                        f.root.remove();
                        delete that.certs[val];
                    }
                }
            });
        });

        this.addButtons();
    },
    addButtons: function () {
        var that = this;
        var c = $('<span ><input type="checkbox" value="" name="Agree" id="Agree-bRGuUvNR"><label for="Agree-bRGuUvNR">同意相关条款</label></span>');
        this.controls.buttons.append(c);

        var b1 = $("<button>").html("清空");
        this.controls.buttons.append(b1);
        b1.kendoButton({
            click: function (e) {
                that.formBasic.setValues({});
                that.formConnect.setValues({});
                that.formCert.setValues({});
                if (that.formCert.certs) {
                    for (var c in that.formCert.certs) {
                        that.formCert.certs[c].setValues({});
                    }
                }
            }
        });

        var b2 = $("<button>").html("重置");
        this.controls.buttons.append(b2);
        b2.kendoButton({
            click: function (e) {
                that.formBasic.setValues(that.formBasic.data);
                that.formConnect.setValues(that.formConnect.data);
                that.formCert.setValues(that.formCert.data);
                if (that.formCert.certs) {
                    for (var c in that.formCert.certs) {
                        that.formCert.certs[c].setValues(that.formCert.certs[c].data);
                    }
                }
            }
        });

        var b3 = $("<button>").html("确定");
        this.controls.buttons.append(b3);
        b3.kendoButton({
            click: function (e) {
                if (that.controls.buttons.find("input:checked").length == 0) {
                    $("<span class='notification error'></span>").html("请先同意相关条款").kendoPopup({
                        anchor: that.controls.buttons.find("input"),
                        position: "top left"
                    }).data("kendoPopup").open();
                    return;
                }

                var dataBasic = that.formBasic.getValues();
                var dataConnect = that.formConnect.getValues();
                var dataCerts = [];
                if (that.formCert.certs) {
                    for (var c in that.formCert.certs) {
                        var cData = that.formCert.certs[c].getValues();
                        dataCerts.push(cData);
                    }
                }

                var allData = XBox.apply({}, dataBasic, dataConnect, {Certs: dataCerts});

                console.log(allData);
            }
        });
    }
});
